import pygame
import Image
from pygame.locals import *
import sys

import opencv
#this is important for capturing/displaying images
from opencv import highgui

from PIL import Image

from socket import *
from select import *

# Make an internet socket, TCP connection mode
s = socket(AF_INET,SOCK_STREAM)
# Bind locally, port 9999
s.bind(('', 6008))
# Start listening, only accept up to 32 connections
s.listen(1)
# Until we feel like stopping,

(conn, addr) = s.accept()

pygame.init()
window = pygame.display.set_mode((640,480))
pygame.display.set_caption("WebCam Demo")
screen = pygame.display.get_surface()
while True:
    #print 'Start'
    #mode = conn.recv(1000)
    mode = "RGB"
	#print(mode)
    #size = (int(conn.recv(1000)),int(conn.recv(1000)))
    size = (640,480)
    #print(size)
    #data = conn.recv(10000000)
    data = ""
    while len(data)<921600:
        data += conn.recv(921600 - len(data))
        print len(data)
	print "Complete: "+str(len(data))
    pg_img = pygame.image.frombuffer(data, size, mode)
    screen.blit(pg_img, (0,0))
    pygame.display.flip()


